﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Text;

using FRL.DebtChased.Extensions;

namespace FRL.DebtChased.Repository.ViewModels
{

	/// <summary>
	/// Declaration of ErrorLogModel class
	/// </summary>
	public class ErrorLogModel
	{

		/////////////////////////////////////////////////////////////
		// Nested classes

		public class Element
		{

			/////////////////////////////////////////////////////////
			// Attributes

			internal int m_iErrorLogId;
			public int ErrorLogId
			{
				get { return m_iErrorLogId; }
			}

			internal string m_strUserFullName;
			public string UserFullName
			{
				get { return m_strUserFullName; }
			}

			internal DateTime m_dtTimestamp;
			public DateTime Timestamp
			{
				get { return m_dtTimestamp; }
			}
			public string TimestampAsText
			{
				get { return m_dtTimestamp.ToCuteString(); }
			}

			internal string m_strAction;
			public string Action
			{
				get { return m_strAction; }
			}

			internal string m_strError;
			public string Error
			{
				get { return m_strError; }
			}
			public string ErrorAsText
			{
				get {
					StringBuilder sb = new StringBuilder(m_strError);
					sb = sb.Replace("\n", "_");
					sb = sb.Replace("\r", "_");
					sb = sb.Replace(" ", "_");
					sb = sb.Replace("___", "_");
					sb = sb.Replace("__", "_");
					sb = sb.Replace("_", " ");
					return sb.ToString(); 
				}
			}

		}

		/////////////////////////////////////////////////////////////
		// Attributes

		private List<Element> m_elements = new List<Element>();
		public List<Element> Elements
		{
			get { return m_elements; }
		}

		/////////////////////////////////////////////////////////////
		// Construction

		public ErrorLogModel(DebtChasedEntities ent)
		{

			// Load error log
			var results = ent.ErrorLogProc();
			foreach (var result in results)
			{

				// Add a new element
				m_elements.Add(new Element() {
					m_iErrorLogId = result.fdErrorLogId,
					m_dtTimestamp = result.fdTimestamp, 
					m_strAction = result.fdAction,
					m_strError = result.fdError, 
					m_strUserFullName = result.fdFullName
				});

			}

		}

	}

}
